﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MalkiSumWeb.Models;

namespace MalkiSumWeb.Controllers
{
    public class MainController : Controller
    {
        public static string myUserName = "";
        public static int myUserID = 0;
        public static string myFirstName = "";
        public static string myLastName = "";
        public static string myPersonalImageUrl = "";
        public static bool IsAuthenticated = false;
        public static bool myGender = false;
        public static DateTime? myCreatedDate = DateTime.Now;

        public static bool ValidateUser(string username, string password)
        {
            SumrizDBEntities db = new SumrizDBEntities();
            var memb = db.Members.Where(m => m.Username == username.Trim() && m.CurrentPassword == password.Trim()).FirstOrDefault();
            if (memb != null)
            {
                if (memb.MemberStatusID.HasValue && memb.IsDeleted == false 
                    && memb.MemberStatusID != 10 && memb.MemberStatusID != 30)
                {
                    return FillUser(memb.ID);
                }
            }
            return false;
        }

        public static bool FillUser(int ID)
        {
            SumrizDBEntities db = new SumrizDBEntities();
            var memb = db.Members.Where(m => m.ID == ID).FirstOrDefault();
            if (memb != null)
            {
                IsAuthenticated = true;
                myUserName = memb.Username;
                myUserID = memb.ID;
                myFirstName = memb.FirstName;
                myLastName = memb.LastName;
                myPersonalImageUrl = memb.PersonalImageUrl;
                myCreatedDate = memb.CreatedDate;
                myGender = memb.Gender.HasValue ? memb.Gender.Value : true;
                return true;
            }
            return false;
        }
    }
}